﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public class StringUtil
    {
        private StringUtil()
        {
        }

        public static string ToCamel(string name)
        {
            string s = name.TrimStart('_');
            s = RemoveSpaces(ToProperCase(s));
            return $"{char.ToLower(s[0])}{s.Substring(1, s.Length - 1)}";
        }

        public static string ToCapit(string name)
        {
            string s = name.TrimStart('_');
            return RemoveSpaces(ToProperCase(s));
        }

        public static string RemoveFinalChar(string s)
        {
            if (s.Length > 1)
            {
                s = s.Substring(0, s.Length - 1);
            }
            return s;
        }

        public static string RemoveFinalComma(string s)
        {
            if (s.Trim().Length > 0)
            {
                int num = s.LastIndexOf(",");
                if (num > 0)
                {
                    s = s.Substring(0, s.Length - (s.Length - num));
                }
            }
            return s;
        }

        public static string RemoveSpaces(string s)
        {
            s = s.Trim();
            s = s.Replace(" ", "");
            return s;
        }

        public static string ToProperCase(string s)
        {
            string result = "";
            if (s.Length > 0)
            {
                if (s.IndexOf(" ") > 0)
                {
                    result = Strings.StrConv(s, VbStrConv.ProperCase, 1033);
                }
                else
                {
                    string text = s.Substring(0, 1).ToUpper(new CultureInfo("en-US"));
                    result = text + s.Substring(1, s.Length - 1);
                }
            }
            return result;
        }

        public static string ToTrimmedProperCase(string s)
        {
            return RemoveSpaces(ToProperCase(s));
        }

        public static string ToString(object o)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Type type = o.GetType();
            PropertyInfo[] properties = type.GetProperties();
            stringBuilder.Append("Properties for: " + o.GetType().Name + Environment.NewLine);
            PropertyInfo[] array = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                try
                {
                    stringBuilder.Append("\t" + propertyInfo.Name + "(" + propertyInfo.PropertyType.ToString() + "): ");
                    if (propertyInfo.GetValue(o, null) != null)
                    {
                        stringBuilder.Append(propertyInfo.GetValue(o, null).ToString());
                    }
                }
                catch
                {
                }
                stringBuilder.Append(Environment.NewLine);
            }
            FieldInfo[] fields = type.GetFields();
            FieldInfo[] array2 = fields;
            foreach (FieldInfo fieldInfo in array2)
            {
                try
                {
                    stringBuilder.Append("\t" + fieldInfo.Name + "(" + fieldInfo.FieldType.ToString() + "): ");
                    if (fieldInfo.GetValue(o) != null)
                    {
                        stringBuilder.Append(fieldInfo.GetValue(o).ToString());
                    }
                }
                catch
                {
                }
                stringBuilder.Append(Environment.NewLine);
            }
            return stringBuilder.ToString();
        }

        public static ArrayList ExtractInnerContent(string content, string start, string end)
        {
            int num = -1;
            int num2 = -1;
            int num3 = -1;
            int num4 = 0;
            ArrayList arrayList = new ArrayList();
            num = content.IndexOf(start);
            num3 = num + start.Length;
            num2 = content.IndexOf(end, num3);
            num4 = num2 - num3;
            if (num >= 0 && num2 > num)
            {
                arrayList.Add(content.Substring(num3, num4));
            }
            while (num >= 0 && num2 > 0)
            {
                num = content.IndexOf(start, num2);
                if (num > 0)
                {
                    num2 = content.IndexOf(end, num);
                    num3 = num + start.Length;
                    num4 = num2 - num3;
                    if (num3 > 0 && num2 > 0)
                    {
                        arrayList.Add(content.Substring(num3, num4));
                    }
                }
            }
            return arrayList;
        }

        public static ArrayList ExtractOuterContent(string content, string start, string end)
        {
            int num = -1;
            int num2 = -1;
            ArrayList arrayList = new ArrayList();
            num = content.IndexOf(start);
            num2 = content.IndexOf(end);
            if (num >= 0 && num2 > num)
            {
                arrayList.Add(content.Substring(num, num2 + end.Length - num));
            }
            while (num >= 0 && num2 > 0)
            {
                num = content.IndexOf(start, num2);
                if (num > 0)
                {
                    num2 = content.IndexOf(end, num);
                    if (num > 0 && num2 > 0)
                    {
                        arrayList.Add(content.Substring(num, num2 + end.Length - num));
                    }
                }
            }
            return arrayList;
        }

        public static string RemovePrefix(string content, string prefixString)
        {
            if (string.IsNullOrEmpty(prefixString) || prefixString.Trim() == string.Empty)
            {
                return content;
            }
            char[] array = new char[3] { ',', ';', ' ' };
            string text = content;
            prefixString = prefixString.Trim(array);
            string[] array2 = prefixString.Split(array);
            string[] array3 = array2;
            foreach (string text2 in array3)
            {
                if (text.IndexOf(text2, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    text = text.Substring(text2.Length);
                    break;
                }
            }
            return text;
        }
    }
}
